home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 235_02 / pathopen.c < prev    next >
Text File  |  1987-06-16  |  2KB  |  53 lines

  1. /*  002  14-Feb-87  pathopen.c
  2.  
  3.         Pathopen will open a file somewhere along the PATH.
  4.  
  5.         Copyright (c) 1987 by Blue Sky Software.  Alll rights reserved.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include "ov.h"          /* only needed for Strdup() define */
  10.  
  11. FILE *
  12. pathopen(fn,mode)      /* open a file somewhere along the path */
  13. char *fn;
  14. char *mode;
  15. {
  16.    FILE *fp;
  17.    int lastch;
  18.    register char *path, *sp;
  19.    char *psave, fname[100], *strchr(), *getenv();
  20.  
  21.    if ((fp = fopen(fn,mode)) == NULL)          /* try to open in current dir */
  22.  
  23.       if (path = getenv("PATH")) {             /* didn't open, any PATH? */
  24.          path = psave = Strdup(path);          /* need zapable copy of PATH */
  25.  
  26.          while (strlen(path)) {                /* while something to check... */
  27.  
  28.             if (sp = strchr(path,';'))         /* ; seperates dir names in */
  29.                *sp = '\0';                     /*  PATH, only chk 1 at a time */
  30.  
  31.             strcpy(fname,path);                /* build file name to chk for */
  32.  
  33.             if ((lastch = fname[strlen(fname)-1]) != '\\' && lastch != '/' &&
  34.                  lastch != ':')
  35.                strcat(fname,"\\");             /* add \ if not already a dir */
  36.  
  37.             strcat(fname,fn);                  /* add help name to dir name */
  38.  
  39.             if (fp = fopen(fname,mode))        /* we're done if it opened */
  40.                break;
  41.  
  42.             if (sp)                    /* if there was a ; there might be */
  43.                path = sp + 1;          /*   another dir in the path to chk */
  44.             else
  45.                break;                  /*   otherwise, just give up */
  46.          }
  47.  
  48.          free(psave);                  /* clean up after ourselves */
  49.       }
  50.  
  51.    return(fp);                 /* return whatever we got */
  52. }
  53.